package ems.dev.easycode.domain.builder.impl;

import cn.hutool.core.util.StrUtil;
import com.google.common.base.CaseFormat;
import ems.dev.easycode.common.FreemarkerEntity;
import ems.dev.easycode.domain.builder.DomainBuilder;
import ems.dev.easycode.domain.entity.DomainEntity;
import ems.dev.easycode.domain.entity.FieldEntity;
import ems.dev.easycode.domain.entity.TableEntity;
import ems.dev.easycode.util.*;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 狠穿
 * @version 1.0.0
 * @date 2023/5/24
 * @function 实体类业务层实现
 */
public class DomainBuilderImpl implements DomainBuilder {

    /**
     * 根据数据库表信息和配置文件信息，生成实体类信息
     * @param schema schema名称
     * @param table 表名称
     * @return DomainEntity
     */
    @Override
    public DomainEntity getDomainInfo(String schema, String table) {
        try {
            TableEntity tableInfo = Db2Util.getTableInfo(schema, table);
            DomainEntity domain = new DomainEntity();
            domain.setName(FormatUtil.tableNameToDomainName(tableInfo.getName()));
            domain.setDescription(tableInfo.getComment().replace("表",StrUtil.EMPTY));
            domain.setAuthor(PropertiesUtil.getProValueStr(Constant.EASY_CODE_PROPERTIES_FILE_NAME,Constant.EASYCODE_CLASS_AUTHOR));
            domain.setPackageName(PackageUtil.getLastLevelPackage(PropertiesUtil.getProValueStr(Constant.EASY_CODE_PROPERTIES_FILE_NAME, Constant.EASYCODE_DOMAIN_PACKAGE_NAME)));
            domain.setInitMetaData(Boolean.valueOf(PropertiesUtil.getProValueStr(Constant.EASY_CODE_PROPERTIES_FILE_NAME,Constant.EASYCODE_DOMAIN_INITMETADATA)));
            List<FieldEntity> fields = tableInfo.getColumns().stream().map(columnEntity -> {
                FieldEntity fieldEntity = new FieldEntity();
                fieldEntity.setName(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnEntity.getColumn()));
                fieldEntity.setType(PackageUtil.getPackageLastPart(columnEntity.getJavaType()));
                fieldEntity.setRemark(StrUtil.isBlank(columnEntity.getComment())?"":columnEntity.getComment());
                fieldEntity.setPk(columnEntity.getPk());
                return fieldEntity;
            }).collect(Collectors.toList());
            domain.setFields(fields);
            return domain;
        } catch (SQLException e) {
            throw new RuntimeException(StrUtil.format("实体类信息生成失败，原因：{}",e.getMessage()));
        }
    }

    /**
     * 基于生成好的实体类信息，使用freemarker模板，生成对应的实体类文件
     * @param domainEntity 实体类对象
     */
    @Override
    public void generateDomainFile(DomainEntity domainEntity) {
        FreemarkerEntity config = new FreemarkerEntity();
        config.setTemplateName("domain.ftl");
        String generateLocation = PropertiesUtil.getProValueStr(Constant.EASY_CODE_PROPERTIES_FILE_NAME, Constant.EASYCODE_GERENATE_LOCATION);
        config.setOutputPath(StrUtil.isBlank(generateLocation)?"C:\\":generateLocation);//不配置默认生成到c盘
        config.setOutputName(domainEntity.getName()+".java");
        config.setDataSource(domainEntity);
        FreemarkerUtil.generateFinalFile(config);
    }

    @Override
    public void createDomain(String tableName){
        try{
            String schema = Db2Util.getCurSchema();
            DomainBuilder r = new DomainBuilderImpl();
            DomainEntity domainInfo = r.getDomainInfo(schema, tableName);
            r.generateDomainFile(domainInfo);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
}
